package nz.ac.massey.cs.se400_2013.ass3.gui;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import nz.ac.massey.cs.se400_2013.ass3.EditorPlugin;
import nz.ac.massey.cs.se400_2013.ass3.plugin.ChangeListener;


public class DefaultEditor implements EditorPlugin  {

	JPanel editor = new JPanel();
	Object object;
	
	public JLabel getLabel(Field field){
		String name = field.getName();
		return new JLabel(name);
	}
	
	public JTextField getEntry(Object object, Field field) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		Method getter = null;
		if(!(field.getType().isPrimitive() ||  field.getType().toString().equals(String.class.toString()))){
			return null;
		}
		JTextField text = new JTextField();
		try{
		getter = object.getClass().getMethod("get" + String.valueOf(field.getName().charAt(0)).toUpperCase() + field.getName().substring(1));
		}
		catch(Exception e){
			getter = object.getClass().getMethod("is" + String.valueOf(field.getName().charAt(0)).toUpperCase() + field.getName().substring(1));
		}
		Object o = getter.invoke(object);
		if(o == null){
			text.setText("");
		}
		else{
			text.setText(getter.invoke(object).toString());
		}
		
		text.setEditable(false);
		return text;
	}
	
	public void fillPanel(Object object){
		try{
		int y = 0;
		
		for(Field field : object.getClass().getDeclaredFields()){
			if(field.getType().isPrimitive() || field.getType().toString().equals(String.class.toString())){
			
			JLabel label = getLabel(field);
			JTextField text = getEntry(object, field);
			GridBagConstraints constraints = new GridBagConstraints();
			constraints.anchor = GridBagConstraints.SOUTHWEST;
			constraints.gridx = 0;
			constraints.gridy = y;
			constraints.ipadx = 50;
			editor.add(label, constraints);
			
			constraints.gridx = 1;
			
			constraints.fill = GridBagConstraints.HORIZONTAL;
			constraints.anchor = GridBagConstraints.SOUTHWEST;
			
			editor.add(text, constraints);
			
			y+=1;
			}
		}
		}
		catch(Exception e){
			System.out.println(e.getMessage().toString());
		}
	} 
	
	public JPanel getEditor(){
		return editor;
	}

	public void clearEditor(){
		for(Component component : editor.getComponents()){
			editor.remove(component);
		}
	}
	
	public void edit(Object object) {
		this.object = object;
		this.clearEditor();
		int height = object.getClass().getDeclaredFields().length;
		GridBagLayout layout = new GridBagLayout();
		editor.setLayout(layout);
		System.out.println(object);
		fillPanel(object);
	}

	
	public boolean canEditInstanceOf(Class type) {
		return true;
	}

	@Override
	public void saveObject() {
		
		
	}

	@Override
	public void addChangeListener(ChangeListener listener) {
		//cant do anything anyway :D
	}

}
